package com.md.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.annotation.Scope;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.servlet.ModelAndView;

import com.md.model.BaseModel;
import com.md.service.BaseService;

@Scope("prototype")
@SuppressWarnings({ "unchecked", "rawtypes", "unused" })
public class BaseController {
	protected Map<String,String> mes = new HashMap<String, String>();// 消息
	protected String message = "";// 记录操作结果信息
	protected int resForFinally = 0;// 默认成功,前台返回成功或者失败
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	
	protected BaseService baseService;

	public BaseService getBaseService() {
		return baseService;
	}

	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	@PersistenceContext
	protected EntityManager entityManager;

	public BaseController() {
		this.setModel();
	}

	protected ModelMap modelMap = null;

	public ModelMap getModelMap() {
		return modelMap;
	}

	public void setModelMap(ModelMap modelMap) {
		this.modelMap = modelMap;
	}

	@ModelAttribute
	public void setReqAndResp(HttpServletRequest request, HttpServletResponse response) {
		this.request = request;
		this.response = response;
	}

	/********************************************************
	 * @Title: getRequest
	 * @Description:返回当前调用方法的类名与方法名，做为VIEW的地址
	 * @return current class/method
	 * @date 2014-8-12 上午12:33:26
	 *********************************************************/
	public ModelAndView display() {
		// String action = new
		// Exception().getStackTrace()[1].getFileName().toString().replace("Controller.java",
		// ""); //action名称
		String[] class_str = (this.getClass().getName()).split("\\.");
		String action = class_str[class_str.length - 1].replace("Controller", "").replace("List", "").replace("Form", "");
		String method = new Exception().getStackTrace()[1].getMethodName().toString(); // 方法名称
		String base = getRequest().getContextPath();
		String fullPath = getRequest().getScheme() + "://" + getRequest().getServerName() + ":" + getRequest().getServerPort() + base;
		setAttribute("base", fullPath); // http://localhost/medicine
		setAttribute("baseClass", fullPath + "/" + action); // http://localhost/finance/action
		setAttribute("url", getRequest().getRequestURL().toString()); // http://localhost:8088/finance/Index/countryPage
		return new ModelAndView(action + "/" + method, this.getModelMap());
	}

	@Deprecated
	public String display(String action, String method) {
		String[] class_str = (this.getClass().getName()).split("\\.");
		String base = getRequest().getContextPath();
		String fullPath = getRequest().getScheme() + "://" + getRequest().getServerName() + ":" + getRequest().getServerPort() + base;
		setAttribute("base", fullPath); // http://localhost/medicine
		setAttribute("baseClass", fullPath + "/" + action); // http://localhost/finance/action
		setAttribute("url", getRequest().getRequestURL().toString()); // http://localhost:8088/finance/Index/countryPage
		return action + "/" + method;
	}

	/**
	 * ********************************************************
	 * 
	 * @Title: setAttribute
	 * @Description: 获取setAttribute
	 * @return void
	 * @date 2014-8-12 上午12:39:55
	 ********************************************************/
	public void setAttribute(String name, Object value) {
		getRequest().setAttribute(name, value);
	}

	/**
	 * ********************************************************
	 * 
	 * @Title: getParameter
	 * @Description: TODO(根据参数名获取值)
	 * @param argName
	 * @return String
	 * @date 2014-9-2 上午11:19:15
	 ********************************************************
	 */
	public String getParameter(String argName) {
		return getRequest().getParameter(argName);
	}

	/**
	 * ********************************************************
	 * 
	 * @Title: getRequest
	 * @Description: 获取request
	 * @return HttpServletRequest
	 * @date 2014-8-12 上午12:59:09
	 ********************************************************
	 */
	public static HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		return request;
	}

	/**
	 * ********************************************************
	 * 
	 * @Title: getSession
	 * @Description: 获取Session
	 * @return Session
	 * @date 2014-8-12 上午12:59:09
	 ********************************************************
	 */

	public static HttpSession getSession() {
		return getRequest().getSession();
	}

	/**
	 * ********************************************************
	 * 
	 * @Title: getResponse
	 * @Description: TODO(获取response)
	 * @return HttpServletResponse
	 * @date 2014-11-1 下午10:43:12
	 ********************************************************
	 */
	public HttpServletResponse getResponse() {
		ServletWebRequest servletContainer = (ServletWebRequest) RequestContextHolder.getRequestAttributes();
		return servletContainer.getResponse();
	}

	/**
	 * ********************************************************
	 * 
	 * @Title: Sucess
	 * @Description: TODO(显示成功信息)
	 * @param message
	 * @return Map<String,String>
	 * @date 2014-9-2 上午10:38:17
	 ********************************************************
	 */
	public Map<String, String> Success(String message, String navTabId) {
		if (message.equals("")) {
			message = "操作成功";
		}
		mes.put("message", message);
		mes.put("statusCode", "200");
		mes.put("navTabId", navTabId);
		return mes;
	}

	/**
	 * ********************************************************
	 * 
	 * @Title: message
	 * @Description: TODO(返回消息到前端)
	 * @param message
	 * @param result
	 * @return Map<String,String>
	 * @date 2014-11-1 下午06:49:18
	 ********************************************************
	 */
	public Map<String, String> message(String message, boolean result) {
		mes.put("statusCode", result ? "200" : "300");
		mes.put("message", message);
		return mes;
	}

	/**
	 * ********************************************************
	 * 
	 * @Title: message
	 * @Description: TODO(这里用一句话描述这个类的作用)
	 * @param message
	 * @param result
	 * @return Map<String,String>
	 * @date 2014-11-1 下午06:53:02
	 ********************************************************
	 */
	public Map<String, String> message(String message, int result) {
		mes.put("statusCode", result == 0 ? "200" : "300");
		mes.put("message", message);
		return mes;
	}


	/**
	 * 
	 ********************************************************* .<br>
	 * [方法] getCurrentTime <br>
	 * [描述] TODO(这里用一句话描述这个方法的作用) <br>
	 * [参数] type 0 yyyy-MM-dd HH:mm:ss 1 yyyy-MM-dd hh:mm:ss 2 yyyy-MM-dd <br>
	 * [返回] String <br>
	 * [时间] 2015-4-16 上午09:57:38 <br>
	 ********************************************************* .<br>
	 */
	public String getCurrentTime(int type) {
		String dateType = "";
		switch (type) {
		case 0:
			dateType = "yyyy-MM-dd HH:mm:ss";
			break;
		case 1:
			dateType = "yyyy-MM-dd hh:mm:ss";
			break;
		case 2:
			dateType = "yyyy-MM-dd";
			break;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(dateType);
		String dateTime = sdf.format(new Date());
		return dateTime;
	}

	// 设置当前的url路径
	public String setBaseUrl() {
		return getRequest().getScheme() + "://" + getRequest().getServerName() + ":" + getRequest().getLocalPort() + getRequest().getContextPath();
	}

	// ---------------------------------------

	public Map getMes() {
		return mes;
	}

	public void setMes(Map mes) {
		this.mes = mes;
	}

	/**
	 * @author yehao
	 * @param baseModel
	 */
	public void setModel() {
		BaseModel baseModel = new BaseModel();
		if (modelMap == null) {
			modelMap = new ModelMap();
			modelMap.addAttribute("model", baseModel);
		}
	}

}
